package xin.alum.aim.redis;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import xin.alum.aim.constant.AimConstant;
import xin.alum.aim.constant.Constants;
import xin.alum.aim.groups.SessionGroups;
import xin.alum.aim.groups.Sessions;

@ConditionalOnProperty(prefix = AimConstant.CLUSTER_PROPERTIES_PREFIX, name = AimConstant.CLUSTER_PROPERTIES_MODE, havingValue = "redis")
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig {

    @Bean
    RedisTemplate<String, Object> aimRedisTemplate(RedisConnectionFactory factory, Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }


    @Bean
    public KickMessageListener kickMessageListener(Sessions sessions,Jackson2JsonRedisSerializer<Object> json) {
        return new KickMessageListener(sessions,json);
    }

    @Bean
    public PushMessageListener pushMessageListener(SessionGroups groups) {
        return new PushMessageListener(groups);
    }

    @Bean
    public ReplyMessageListener replyMessageListener(SessionGroups groups,Sessions sessions) {
        return new ReplyMessageListener(groups,sessions);
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory,
                                                                       PushMessageListener pushMessageListener,
                                                                       KickMessageListener kickMessageListener,
                                                                       ReplyMessageListener replyMessageListener) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        if (!connectionFactory.getConnection().isClosed()) {
            container.setConnectionFactory(connectionFactory);
            container.addMessageListener(pushMessageListener, new ChannelTopic(Constants.PUSH_MESSAGE_INNER_QUEUE));
            container.addMessageListener(kickMessageListener, new ChannelTopic(Constants.BIND_MESSAGE_INNER_QUEUE));
            container.addMessageListener(replyMessageListener, new ChannelTopic(Constants.EVENT_MESSAGE_INNER_QUEUE));
        }
        return container;
    }

    @Bean
    public RedisPusher redisPusher(@Qualifier("aimRedisTemplate") RedisTemplate<String, Object> redis) {
        return new RedisPusher(redis);
    }

}
